(function($, im, doc) {
	$.plusReady(function() {
//		document.getElementById("tofiles").addEventListener('tap', function() {
//			var service_comfirm = plus.webview.getWebviewById("myfile.html");
//			service_comfirm && plus.webview.close(service_comfirm, "none");
//			mui.openWindow({
//				url: "myfile.html",
//				id: "myfile.html"
//			});
//		});

		window.addEventListener('SENDFILE', function(event) {
			var filepath = event.detail.filepath;
			var filename = event.detail.filename;
			var ext = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
			var name = im.uid + '-' + Date.parse(new Date()) + '.' + ext;
			var sname = im.uid + '-' + Date.parse(new Date()) + '_s.' + ext;
			var webpath = event.detail.webpath || 'upload/file/' + name;
			var filesize = event.detail.filesize || 0;
			//console.log(name);
			if(ext == 'jpg' || ext == 'jpeg' || ext == 'gif' || ext == 'png') {
				var sendtype = 'image';
			} else if(ext == 'mp4') {
				var sendtype = 'video';
			} else {
				var sendtype = 'file';
			}
			var msg = {
				type: sendtype,
				content: filepath,
				extras: {
					filename: filename,
					size: filesize,
					filepath: filepath
				}
			};
			im.sendMsg(msg, 1);
			if(sendtype == 'image') {
				im.playsendSound('pic');
			}
			im.closeFooterBox();
			if(!event.detail.webpath) {
				var task = plus.uploader.createUpload(upurl, { //gz-修改
						method: "POST",
						blocksize: 2048000,
						priority: 100
					},
					function(t, status) {
						//var el = doc.querySelector("[data-id='" + webpath + "']");
						//var parents = el.parentNode;
						console.log(status);
						console.log(JSON.stringify(t));
						if(status == 200) {
							//var rt = JSON.parse(t.responseText);
							var files = filepath.split("/");
							var fname = files.pop();
							var exts = name.split(".");
							var ext = exts.pop();
							var msg = {
								type: sendtype,
								content: 'upload/file/' + name,
								extras: {
									filename: filename,
									size: t.totalSize,
									filepath: 'upload/file/' + name,
									name : fname,
									ext : ext,
									src : serverurl + 'upload/file/' + sname,
									bsrc : serverurl + 'upload/file/' + name
								}
							};
							if(sendtype !== 'file') {
								if(sendtype == 'image') {
									msg.content = serverurl + 'upload/file/' + sname;
									//msg.extras = res;
									var el = document.querySelector("[src='" + filepath + "']");
									//res.src = serverurl + res.surl;
									//res.bsrc = serverurl + res.url;
									el.setAttribute('src', res.src);
									el.setAttribute('bsrc', res.bsrc);
									el.setAttribute('fname', res.name);
									el.setAttribute('fsize', res.size);
									el.setAttribute('ftype', res.type);
									var parent = el.parentNode.parentNode.parentNode;
									var id = parent.getAttribute('msg-id');
									var extras = JSON.stringify(res);
									chat.edit(im.roomId, id, 'extras', extras);
								} else {
									msg['content'] = serverurl + 'upload/file/' + name;
								}

							}
							//console.log(JSON.stringify(msg));
							//rt.filename = filename;
							im.sendMsg(msg, 2);
							//el.querySelector('span').innerHTML = rt.size + 'kb';
							mui.toast("发送成功！");
						} else {
							//el.setAttribute('data-path', filepath);
							//console.log(filepath);
							if(sendtype == 'image') {
								im.ajaxuploadImg(filepath);
							}else{
								mui.toast("发送失败！");
							}
							
						}
					},
					function(err) {
						console.log(JSON.stringify(err));
					});

				task.addFile(filepath, {
					key: "file",
					name: name
				});
				//task.addData('fname', name);
				task.start();
			} else {
				mui.toast("发送成功！");
			}

			//var wt = plus.nativeUI.showWaiting();

		});
		$("#msg-list").on('tap', '.file-area', function(e) {
			var url = this.getAttribute('data-id');
			var name = this.getAttribute('data-name');
			var size = this.getAttribute('data-size');
			var path = this.getAttribute('data-path');
			var urls = JSON.parse(localStorage.getItem('myfilelist') || '{}');
			//var rturl = urls[url]['path'];
			//console.log(this.parentNode.className.indexOf('msg-item-self'));
			if(urls[url] && urls[url]['path']) {
				var openpath = urls[url]['path'];
			} else {
				if(this.parentNode.className.indexOf('msg-item-self') === -1) {
					var openpath = undefined;
				} else {
					var openpath = path;
				}
			}
			console.log(openpath);
			if(openpath !== undefined) {
				plus.runtime.openFile(openpath, {}, function(e) { //调用第三方应用打开文件  
					mui.toast('打开失败');

				});
			} else {
				var path = '_downloads/';
				console.log(serverurl + url);
				//创建下载任务
				var dtask = plus.downloader.createDownload(serverurl + url, {
					filename: '_downloads/'
				}, function(d, status) {
					console.log(status);
					if(status == 200) {
						//下载成功
						urls[url] = {
							path: plus.io.convertLocalFileSystemURL(d.filename),
							name: name,
							size: size
						};
						localStorage.setItem('myfilelist', JSON.stringify(urls));
						plus.runtime.openFile(urls[url]['path'], {}, function(e) { //调用第三方应用打开文件  
							mui.toast('打开失败');
						});
					} else {
						mui.toast('文件不存在');
					}
				}).start();
			}

		});
	});

}(mui, im, document));